package com.adfframeworks.labs.controllers;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.adfframeworks.labs.domain.Community;
import com.adfframeworks.labs.service.CommunityService;
import com.adfframeworks.labs.service.EmployeeService;

@Controller
@RequestMapping(value = "/welcome/id={user_id}/community")
public class CommunityController {

	@Autowired
	CommunityService communityService;
	
	@Autowired
	EmployeeService employeeService;

	List<String> visibilities = new ArrayList<String>();

	private final String addNewCommunityView = "newCommunity";

	public CommunityController() {
		visibilities.add("Private");
		visibilities.add("Public Restricted");
		visibilities.add("Public Unrestricted");
	}

	@RequestMapping(value = "/addNewCommunity", method = RequestMethod.GET)
	public ModelAndView addNewCommunity() {
		System.out.println("Display form to add a new community.");
		return new ModelAndView(addNewCommunityView, "community",
				new Community());
	}

	@RequestMapping(value = "/addNewCommunity", method = RequestMethod.POST)
	public String displayCommunity(@ModelAttribute("community") Community community, ModelMap model) {
		model.addAttribute("name", community.getName());
		model.addAttribute("keyword", community.getKeyword());
		return "community";
	}
	
	@RequestMapping(value = "/id={community_id}", method = RequestMethod.GET)
	public String displayCommunity(@PathVariable Integer user_id, @PathVariable Integer community_id, ModelMap model) {
		System.out.println("Display the request information.");
		Community community = communityService.getCommunity(community_id);
		model.addAttribute("user", user_id);
		model.addAttribute("community", community);
		model.addAttribute("owner", employeeService.getEmployee(community.getOwner()));
		return "displayCommunity";
	}
}
